package search.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;

public class Find {
	public static String findAttribute(String tag,String attribute){
		String regex;
		regex = " "+tag+"=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]";
		Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
		Matcher ma = pa.matcher(attribute);
		if(ma.find()){
			return ma.group().replace(" "+tag+"=", "").replace("\"","").replace("'","").trim();
		}
		return null;
	}
	public static String findAttribute(String attribute){
		String regex;
		regex = " "+attribute+"=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]";
		Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
		Matcher ma = pa.matcher(attribute);
		if(ma.find()){
			return ma.group().replace(" "+attribute+"=", "").replace("\"","").replace("'","");
		}
		return "";
	}
	public static String find(String pattern,String string){
		String regex;
		//regex =pattern;
		Pattern pa = Pattern.compile(pattern, Pattern.DOTALL);
		Matcher ma = pa.matcher(string);
		if(ma.find()){
			return ma.group();
		}
		return null;
	}
	

	
	public static void main(String[] args){
		String str=" http-equiv=\"content-type\" content=\"text/html; charset=gb2312\"";
		System.out.println(Find.findAttribute("content", str));
	}
	

}
